package com.caocaog.market.toccpt.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.caocaog.market.toccpt.ccptmodel.CcOrder;
import com.caocaog.market.toccpt.ccptmodel.CouponUse;
import com.caocaog.market.toccpt.ccptmodel.OrderLog;
import com.caocaog.market.toccpt.ccptmodel.UserAccountRecord;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.mapper
 * @ClassName CcptMapper.java
 * @createTime 2021年12月21日 17:25:00
 * @Description 查询曹操跑腿数据库
 */
@DS("sql-server-datasource")
public interface CcptMapper extends BaseMapper<CcOrder> {
    /**
     * 根据跑腿订单id查询该跑腿订单的所有信息
     * @return
     */
    CcOrder findCcptOrderById(@Param("orderId") int orderId);

    /**
     * 根据会员id、代理商id 以入当前日期查询可用的优惠券信息；
     *      只返回一条记录  top 1
     * @param memberId
     * @param agentId
     * @param currentDate
     * @return
     */
    Map<String,Object> findCouponByCondation(@Param("memberId") int memberId,@Param("agentId") int agentId,
                                             @Param("currentDate") Date currentDate,@Param("price") String price);

    /**
     * 根据发单会员id查询发单会员的状态
     * @param memberId
     * @return
     */
    int findMemberStatus(@Param("memberId") int memberId);

    /**
     * 根据代理商id和发单会员id查询该发单会员所属的方案组id
     * @param agentId
     * @param memberId
     * @return
     */
    Map<String,Integer> findBrokerageByAgentIdAndMemberId(@Param("agentId") int agentId,@Param("memberId") int memberId);

    /**
     * 向sqlserver库中插入跑腿订单数据记录
     */
    int insertCcOrder(CcOrder ccOrder);

    int checkCouponById(@Param("currentDate")Date currentDate,@Param("couponId")int couponId);
    int insertCouponUse(CouponUse couponUse);

    /**
     * #{orderSid},#{agentSid},1,#{dis},#{staffids}
     * @return
     */
    int insertRabbitMqRecord(@Param("orderSid") int orderSid,@Param("agentSid") int agentSid,
                             @Param("dis") int dis,@Param("staffids") String staffids);

    Map<String,Object> findMemberForBalance(@Param("sid")int sid);

    int updateBalanceBySid(@Param("balance")String balance,@Param("sid")int sid);

    /**
     * 跑腿系统订单状态
     *      订单状态 1：支付成功待接单，2：已接单，3：骑士到店，4：已取货/配送中，5：已送达，6：订单完成，21：用户取消订单，22：代理商取消订单，23：总后台取消订单
     */
    Map<String,Object> findOrderStatus(@Param("marketOrderNo") String marketOrderNo);

    /**
     * 根据曹操来了的订单号去曹操跑腿查询该订单是否在曹操跑腿中已经存在
     */
    int findForThirdPartyNo(@Param("marketOrderNo") String marketOrderNo);

    /**
     * 用户取消订单时根据订单id查询订单信息
     * 参数：订单id
     */
    CcOrder findCcptOrderForCancelByOrderId(@Param("orderId") int orderId);

    /**
     * 取消曹操跑腿订单，
     * @return
     */
    int updateOrderForCancel(@Param("cancelStatus") int cancelStatus,
                             @Param("cancelTime") Date cancelTime,
                             @Param("orderId") int orderId,
                             @Param("orderStatus") int orderStatus);
    /**
     * 存订单取消的日志记录
     */
    int insertCcOrderLog(OrderLog orderLog);

    /**
     * 更改订单修正数据的状态
     */
    int updateOrderAmend(@Param("orderId") int orderId);

    /**
     * 添加用户余额变动记录
     */
    int insertUserAccountRecord(UserAccountRecord userAccountRecord);

    /**
     * 优惠券退回
     */
    int couponThById(@Param("couponId") int couponId);

    /**
     * 查询代理商资金池余额
     */
    @Select("select cash_pooling_balance from cc_agent where sid=#{agentSid}")
    BigDecimal findAgentCashPoolingBalance(@Param("agentSid") int agentSid);

    /**
     * 更新代理商的资金池余额
     */
    @Update("update cc_agent set cash_pooling_balance=#{nowBalance} where sid=#{agentSid} and cash_pooling_balance=#{balance}")
    int updateAgentCashPoolingBalance(@Param("nowBalance") BigDecimal nowBalance,@Param("agentSid") int agentSid,@Param("balance") BigDecimal balance);

    /**
     * 插入代理商资金池流水记录
     */
    @Insert("insert into cc_agent_account_record(agent_sid,ser_no,before_change,after_change,change_value,change_type,remark,create_date,tempcaocaoid,type) " +
            "values(#{agentSid},#{serNo},#{beforeChange},#{afterChange},#{changeValue},#{changeType},#{remark},#{createDate},0,0)")
    int insertAgentPoolingRecord(@Param("agentSid") int agentSid,
                                 @Param("serNo") String serNo,
                                 @Param("beforeChange") BigDecimal beforeChange,
                                 @Param("afterChange") BigDecimal afterChange,
                                 @Param("changeValue") BigDecimal changeValue,
                                 @Param("changeType") int changeType,
                                 @Param("remark") String remark,
                                 @Param("createDate") Date createDate
                                 );
}
